package io.xxx.xbutler.web.controller.form;

import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.xxx.xbutler.domain.RoomCategory;
import io.xxx.xbutler.domain.SelectMode;
import io.xxx.xbutler.domain.task.ReceiverType;
import io.xxx.xbutler.domain.task.TaskScene;
import jakarta.validation.constraints.Future;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;
import java.util.List;

@Data
@Accessors(chain = true)
public class TaskForm implements FormValidator {

    @NotEmpty(message = "任务名称不能为空")
    protected String name;

    @NotNull(message = "任务场景不能为空")
    protected TaskScene scene;

    @NotNull(message = "任务区域不能为空")
    protected Integer areaId;

    protected Integer operateAreaId;

    protected Long businessUnitId;

    protected String businessUnitName;

    @NotNull(message = "任务开始时间不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected LocalDateTime startTime;

    @NotNull(message = "任务结束时间不能为空")
    @Future(message = "任务结束时间不能小于等于当前时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected LocalDateTime endTime;

    @NotNull(message = "接收人类型不能为空")
    protected ReceiverType receiverType;

    protected RoomCategory roomCategory;

    @NotNull(message = "接收人选择模式为空")
    protected SelectMode selectMode;

    protected JSONObject selectContent;

    /**
     * 忽略门店的推送时间段设置
     */
    protected Boolean ignorePushInterval;

    /**
     * 忽略门店的推送数量上限设置
     */
    protected Boolean ignorePushQuantity;

    /**
     * 如果群没有绑定门店，使用任务指定的门店
     */
    protected Long storeId;

    protected String storeCode;

    protected String storeName;

    protected List<List<TaskItemForm>> items;

    @Override
    public void validate() {
        if (startTime.isAfter(endTime)) {
            throw new IllegalArgumentException("任务结束时间不能小于任务开始时间");
        }
    }
}
